//----------------------------------Include-------------------------------------
#include <string.h>
#include <stdlib.h>
#include "mcu.h"
#include "glbvar.h"
//------------------------------------------------------------------------------
//global var defined in this file
#define  DBG_GLOBALS
#include "dbg.h"
//------------------------------------------------------------------------------

//---------------------------------Global Var-----------------------------------
uint32_t                          gu32_dbg_to;
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
//for rx
#define RXBUF_LEN                 (64)
//------------------------------------------------------------------------------
//for tx
#define _tx_data(p, l)            do{ mcu_uart1_tx((char*)p, l); }while(0)
#define TXBUF_LEN                 (64)
//------------------------------------------------------------------------------

//------------------------------------Type--------------------------------------
typedef struct{
  unsigned int   cnt;
  unsigned int   done;
  char           data[RXBUF_LEN];
}t_rcv;
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
//for rx
static t_rcv            nt_rcv;
//for tx
static char             na_txbuf[TXBUF_LEN];
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
static void _tx_str(char* s)
{
  strcpy(na_txbuf, s);
  _tx_data(na_txbuf, strlen(na_txbuf));
}
//------------------------------------------------------------------------------
static char* _check_rx(uint32_t* sz)
{
  *sz = 0;
  if(!nt_rcv.done) return NULL;
  if(!nt_rcv.cnt ) return NULL;
  *sz = nt_rcv.cnt;
  return &nt_rcv.data[0];
}

static void _clear_rx(void)
{
  nt_rcv.done = 0;
  nt_rcv.cnt  = 0;
}
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void dbg_cyc(void)
{
  if(gu32_dbg_to == DBG_NORMAL_INFO){
    char*     p;
    uint32_t  sz;
  
    p = _check_rx(&sz);
    if(p){
      _clear_rx();
    }
  }
}

void dbg_out(char* s)
{
  if(gu32_dbg_to == DBG_NORMAL_INFO){
    _tx_data(s, strlen(s));
  }
}
//------------------------------------------------------------------------------
#if 1
//for interrupt receive
void it_dbg_rcv_one_byte(unsigned char c)
{
  if(nt_rcv.cnt >= RXBUF_LEN){
    nt_rcv.done = 0;
    nt_rcv.cnt  = 0;
    return;
  }
  nt_rcv.data[nt_rcv.cnt++] = c;

  if(nt_rcv.cnt >= 2){
    if(nt_rcv.data[nt_rcv.cnt-2] == 0x0D){
      if(nt_rcv.data[nt_rcv.cnt-1] == 0x0A){
        nt_rcv.done = 1;
      }else{
        nt_rcv.done = 0;
        nt_rcv.cnt  = 0;
      }
    }
  }
}

void it_dbg_rcv_done(void)
{
  nt_rcv.done = 1;
}
#endif
//------------------------------------EOF---------------------------------------




